﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF_PathSearchUtil.entity;

namespace WPF_PathSearchUtil.utils
{
    internal class ListConvert
    {
        /// <summary>
        ///     将fileList转为用于画面显示的 String列表
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="fileList"></param>
        public static void ListToStr(ref ObservableCollection<ObservableCollection<String>> strs, ObservableCollection<PathInfo> fileList)
        {
            strs.Clear();
            for (int i = 0; i < fileList.Count; i++)
            {
                ObservableCollection<String> list = new ObservableCollection<String>
                {
                    fileList[i].FileName,
                    fileList[i].FilePath,
                    fileList[i].FileType,
                    fileList[i].FileSize + "KB",
                    fileList[i].FileDate.ToString()
                };
                strs.Add(list);
            }
        }

        /// <summary>
        ///     将获得的 画面用list 转为 fileList
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="fileList"></param>
        public static void StrToList(ObservableCollection<ObservableCollection<String>> strs, ref ObservableCollection<PathInfo> fileList)
        {
            foreach (ObservableCollection<String> list in strs)
            {
                PathInfo fileInfo = new PathInfo();
                fileInfo.FileName = list[0];
                fileInfo.FilePath = list[1];
                fileInfo.FileType = list[2];
                fileInfo.FileSize = double.Parse(list[3].Substring(0, list[3].Length - 2));
                fileInfo.FileDate = Convert.ToDateTime(list[4]);
                fileList.Add(fileInfo);
            }


        }

        /// <summary>
        ///     将从服务器端获得的JArray数组转换为 字符串list，用于显示在画面列表中
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="fileList"></param>
        public static void JArrayToList(ref ObservableCollection<ObservableCollection<String>> strs, JArray fileList)
        {
            foreach (JObject fileInfo in fileList)
            {
                ObservableCollection<String> list = new ObservableCollection<String>
                {
                    fileInfo["fileName"].ToString(),
                    fileInfo["filePath"].ToString(),
                    fileInfo["fileType"].ToString(),
                    fileInfo["fileSize"].ToString(),
                    fileInfo["fileDate"].ToString()
                };
                strs.Add(list);
            }
        }
    }
}
